<?php

class fieldComponents extends sfComponents
{  
  public function executeTable_search(sfWebRequest $request)
  { 
    if($this->type == 'text' && $this->subtype != 'date')
    {
      $this->javascriptEvent = 'onkeyup';
    }
    else
    {
      $this->javascriptEvent = 'onchange';
    }
    
    
    if($this->type == 'choice')
    {
      $this->relationName = Cms::getRelationNameFormLocal(sfConfig::get('app_soulution_name') .  $request->getParameter('table'), $this->column);
      
      $this->options = Doctrine_Core::getTable($this->relationName)->findAll();
    }
  }
  
  public function executeImage(sfWebRequest $request)
  {
    $fileBaseSource = $this->getFileBase($this->field);
    
    $this->anachor= $fileBaseSource . '_l.' . $this->file['extension'];
    
    $this->src = $fileBaseSource . '_m.' . $this->file['extension'];
  }  
  
  public function executeImages(sfWebRequest $request)
  {
    $fileBaseSource = $this->getFileBase($this->field);
    
    $this->anachor = $fileBaseSource . '_l.' . $this->file['extension'];
    
    $this->src = $fileBaseSource . ($request->getParameter('action') == 'index' ?  '_s.' : '_m.') . $this->file['extension'];
    
    $this->rel = ($request->getParameter('action') == 'index' ? $this->key : null);
    
    $this->description = (strlen($this->file['description']) > 32 ? substr($this->file['description'], 0, 30) . '...' : $this->file['description']);
    
    if($request->getParameter('action') == 'index')
    {
      echo get_partial('table/image', $this->varHolder->getAll());
    }
    else if ($request->getParameter('action') == 'show')
    {
      echo get_partial('show/image', $this->varHolder->getAll());
    }
    else
    {
      echo get_partial('form/image', $this->varHolder->getAll());
    }
          
    return sfView::NONE;

  }
  
  private function getFileBase($field)
  {
    if($field['subtype'] == 'images' || $field['subtype'] == 'files')
    {
      $tabDir = $field['tab'];
    }
    else
    {
      $tabDir = $field['relation'];
    }
    
    return DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . $tabDir. DIRECTORY_SEPARATOR . $this->file['name'];
  }
}